package com.sl.au.repository;

import java.util.List;

//import org.activiti.engine.impl.Page;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
import com.sl.au.entity.Option;
import com.sl.au.entity.SaleArea;

@Transactional(readOnly = true)
public interface SaleAreaRepository extends JpaRepository<SaleArea, String> {
	@Query(" select m from Module m where m.moduleName like '%'+?1+'%' or m.name like '%'+?1+'%' ")
	Page<SaleArea> getModuleName(String key, Pageable pageable);

	/**
	 * 获取页面数据
	 * 
	 * @param key
	 * @param pageable
	 * @return
	 */
	@Query("select s from SaleArea s where s.isAbandon = 0 and (s.areaCode like '%'+?1+'%' or s.areaName like '%'+?1+'%' "
			+ "or s.superior like '%'+?1+'%')")
	Page<SaleArea> findSaleArea(String key, Pageable pageable);

	/**
	 * 获取页面数据数量
	 * 
	 * @param key
	 * @param pageable
	 * @return
	 */
	@Query("select count(*) from SaleArea s where s.isAbandon = 0 and (s.areaCode like '%'+?1+'%' or s.areaName like '%'+?1+'%' "
			+ "or s.superior like '%'+?1+'%')")
	Long getSaleAreaCount(String key);

	/**
	 * 从Option表获取上级部门的下拉框选项
	 * 
	 * @param superior
	 * @return
	 */
	@Query("select o from Option o where o.group = ?1")
	List<Option> findByGroup(String superior);

	/**
	 * 获取片区信息
	 * 
	 * @param key
	 * @param pageable
	 * @return
	 */
	@Query("select s from SaleArea s where s.isAbandon = 0 and s.superior = ?1")
	List<SaleArea> findBySuperior(String superior);

	/**
	 * 获取所有片区（除废弃外）
	 * 
	 * @param key
	 * @param pageable
	 * @return
	 */
	@Query("select s from SaleArea s where s.isAbandon = 0")
	List<SaleArea> getAllSaleArea();

	/*
	 * 部门与片区的唯一验证
	 */
	@Query("select count(*) from SaleArea s where s.isAbandon = 0 and s.superiorArea = ?1")
	long getRearea(String superiorArea);

	/**
	 * 根据部门片区找到记录
	 * 
	 * @param superiorArea
	 * @return
	 */
	SaleArea findBySuperiorArea(String superiorArea);
}
